package com.chenyaowei.web.controller.marketing.coupon;

import cn.hutool.core.util.IdUtil;
import com.chenyaowei.common.annotation.Log;
import com.chenyaowei.common.core.controller.BaseController;
import com.chenyaowei.common.core.domain.AjaxResult;
import com.chenyaowei.common.core.page.TableDataInfo;
import com.chenyaowei.common.enums.BusinessType;
import com.chenyaowei.common.utils.poi.ExcelUtil;
import com.chenyaowei.marketing.domain.CywCouponInfo;
import com.chenyaowei.marketing.service.ICywCouponInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 优惠券信息Controller
 *
 * @author 陈耀威
 * @date 2023-11-28
 */
@RestController
@RequestMapping("/marketing/couponInfo")
public class CywCouponInfoController extends BaseController
{
    @Autowired
    private ICywCouponInfoService cywCouponInfoService;

    /**
     * 前台优惠券信息列表
     */
    @GetMapping("/front/list")
    public TableDataInfo frontList(CywCouponInfo cywCouponInfo)
    {
        startPage();
        List<CywCouponInfo> list = cywCouponInfoService.selectFrontList(cywCouponInfo);
        return getDataTable(list);
    }

    /**
     * 查询优惠券信息列表
     */
//    @PreAuthorize("@ss.hasPermi('coupon:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(CywCouponInfo cywCouponInfo)
    {
        startPage();
        List<CywCouponInfo> list = cywCouponInfoService.selectCywCouponInfoList(cywCouponInfo);
        return getDataTable(list);
    }

    /**
     * 导出优惠券信息列表
     */
    @PreAuthorize("@ss.hasPermi('coupon:info:export')")
    @Log(title = "优惠券信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CywCouponInfo cywCouponInfo)
    {
        List<CywCouponInfo> list = cywCouponInfoService.selectCywCouponInfoList(cywCouponInfo);
        ExcelUtil<CywCouponInfo> util = new ExcelUtil<CywCouponInfo>(CywCouponInfo.class);
        util.exportExcel(response, list, "优惠券信息数据");
    }

    /**
     * 获取优惠券信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('coupon:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cywCouponInfoService.selectCywCouponInfoById(id));
    }

    /**
     * 新增优惠券信息
     */
    @PreAuthorize("@ss.hasPermi('coupon:info:add')")
    @Log(title = "优惠券信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CywCouponInfo cywCouponInfo)
    {
        cywCouponInfo.setCode(IdUtil.simpleUUID());
        return toAjax(cywCouponInfoService.insertCywCouponInfo(cywCouponInfo));
    }

    /**
     * 修改优惠券信息
     */
    @PreAuthorize("@ss.hasPermi('coupon:info:edit')")
    @Log(title = "优惠券信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CywCouponInfo cywCouponInfo)
    {
        return toAjax(cywCouponInfoService.updateCywCouponInfo(cywCouponInfo));
    }

    /**
     * 删除优惠券信息
     */
    @PreAuthorize("@ss.hasPermi('coupon:info:remove')")
    @Log(title = "优惠券信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cywCouponInfoService.deleteCywCouponInfoByIds(ids));
    }
}
